package com.appspot.didit.server;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;

import com.appspot.didit.client.tag.Tag;
import com.appspot.didit.client.tag.TagManager;

public class UtilServer {

	final static int MIN_FONT_SIZE = 10;   
	final static int MAX_FONT_SIZE = 20;
	
	public static String[] splitStringInArray(String inputStr) {
	    return inputStr.split(" ");
	}
	
	public static int getTagSize(Tag t) {
		int frequency = t.getItem_id().size();
		int min = 0;
		int max = TagManager.getInstance().MAX_FREQUENCY;
		if(frequency == max) {
			return MAX_FONT_SIZE;
		}
		if(frequency == min) {
			return MIN_FONT_SIZE;
		}
		double weight = (Math.log(frequency) - Math.log(min)) / (Math.log(max) - Math.log(min));
	    return MIN_FONT_SIZE + (int)Math.round((MAX_FONT_SIZE - MIN_FONT_SIZE) * weight);
	}
	public static SimpleDateFormat date = new SimpleDateFormat("dd.MM.");
	public static SimpleDateFormat completeDate = new SimpleDateFormat("dd.MM.yyyy");
	public static SimpleDateFormat monthWithCompleteYear = new SimpleDateFormat("MMM.yyyy");
	public static SimpleDateFormat weekWithCompleteYear = new SimpleDateFormat("ww.yyyy");
	
	public final static long MilisecondsPerDay = 86400000L;
	public final static long MilisecondsPerWeek = 604800000L;
	
	public static String getCompleteDate(Date date1) {
		return completeDate.format(date1);
	}
	
	public static String getMonthWithCompleteYear(Date date1) {
		return monthWithCompleteYear.format(date1);
	}
	
	public static String getWeekWithCompleteYear(Date date1) {
		String week = weekWithCompleteYear.format(date1);
		String result = week.substring(0, 2) + ".KW ";
		if(!week.startsWith("01")) {
			result += week.substring(3);
		} else {
			Date lastDayOfTheWeek = new Date(date1.getTime() + (6 - date1.getDay()) * MilisecondsPerDay);
			result += weekWithCompleteYear.format(lastDayOfTheWeek).substring(3);
		}
		return result;
	}
	
	@SuppressWarnings("deprecation")
	public static String getDate(Date date1) {
		Date now = new Date();
		if(date1.after(now)) {
			return "Zukunft";
		}
		if(now.getHours() == 23) {
			now.setTime(now.getTime() + 60*60*1000L);
		}
		now.setHours(23);
		now.setMinutes(59);
		now.setSeconds(59);
		
		Date today = new Date(now.getTime() - 24*60*60*1000);
		if(date1.after(today)) {
			return "Heute";
		}
		
		Date yesterday = new Date(now.getTime() - 48*60*60*1000);
		
		if(date1.after(yesterday)) {
			return "Gestern";
		}
		
		Date tby = new Date(now.getTime() - 72*60*60*1000);
		
		if(date1.after(tby)) {
			return "Vorgestern";
		}
		return date.format(new Date(date1.getTime() + 60*60*1000));
	}
	
	public static String generateKey(int length) {
		int codelen = length;
		String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
		Random random = new Random(System.currentTimeMillis());
		StringBuffer sb = new StringBuffer(codelen);
		for (int n = 0; n < codelen; n++) {
			int i = random.nextInt(chars.length());
			sb.append(chars.charAt(i));
		}
		return sb.toString();
	}
	
	public static String generateKey() {
		return generateKey(10);
	}
	
	public static String encrypt(String value, String key) {
		return value;
	}
	
	public static String decrypt(String value, String key) {
		return value;
	}
	
	public static double round(double num) {
		double result = (int)(num * 100.0 + 0.5);
		result = result / 100.0;
		return result;
	}
	
	public static String getDateString(int interval, Date date) {
		String result = "";
		switch(interval) {
		case 0:
			result = getDate(date);
			break;
		case 1:
			result = getWeekWithCompleteYear(date);
			break;
		case 2:
			result = getMonthWithCompleteYear(date);
			break;
		default:
			result = getDate(date);
		}
		return result;
	}
	
	public static Date getNextDate(int interval, Date date) {
		Date result;
		switch(interval) {
		case 0:
			result = new Date(date.getTime() + MilisecondsPerDay);
			break;
		case 1:
			result = new Date(date.getTime() + MilisecondsPerWeek);
			break;
		case 2:
			result = new Date();
			result.setYear(date.getYear());
			result.setDate(1);
			result.setMonth(date.getMonth() + 1);
			break;
		default:
			result = new Date();
			break;
		}
		return result;
	}
}
